package com.itheima.CSM_bacK.aaaControllers;

import com.itheima.CSM_bacK.aabServices.Serv.CheckSetmealServ;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.DTO.DTOcSetmealA;
import com.itheima.pojo.DTO.DTOcSetmealE;
import com.itheima.pojo.VO.VOcSetmealFBI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class CheckSetmealController {

    @Autowired
    private CheckSetmealServ checkSetmealServ;
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody DTOcSetmealA kk) {
        checkSetmealServ.add(checkgroupIds, kk);

        String newImg = kk.getImg();
        //DB_Redis
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).add(newImg);
        //aliyun_Redis
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(newImg);
        return new Result(true, "新增套餐成功");
    }

    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds, @RequestBody DTOcSetmealE kk) {
        VOcSetmealFBI setmeal = checkSetmealServ.findById(kk.getId());
        String oldImg = setmeal.getImg();
        String newImg = kk.getImg();

        checkSetmealServ.edit(checkGroupIds, kk);

        //DB_Redis
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).add(newImg);
        //oldImg是昨天的还是今天的？
        if (redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).members().contains(oldImg)) {
            redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).remove(oldImg);
        } else {
            redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(oldImg);
        }
        //aliyun_Redis
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(newImg);

        return new Result(true, "编辑套餐成功");
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        VOcSetmealFBI kk = checkSetmealServ.findById(id);
        return new Result(true, "查询套餐成功", kk);
    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean kk) {
        PageResult kks = checkSetmealServ.findPage(kk);
        return kks;
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile imgFile) throws Exception {
        String name = imgFile.getOriginalFilename();
        String ext = name.substring(name.lastIndexOf("."));
        String newName = UUID.randomUUID() + ext;
        String url = aliOssUtil.upload(imgFile.getBytes(), newName);

//        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).add(url);
//        System.out.println("已上传图片到DB_redis,aliyun:"+url);
        return new Result(true, "图片上传成功", url);
    }


}
